package me.zixing.memorandum.utils;

import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.ui.component.RemindPanel;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

public class TimingUtils {

    public static Set<DayTask> dayTasks = new CopyOnWriteArraySet<>();//符合条件的待提醒tasks
    private static volatile int i = 0;
    private static JViewport jViewport;
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
    private static SimpleDateFormat allFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static volatile boolean flag = true;
    private static Set<DayTask> showedTasks = new TreeSet<>();

    public static synchronized void setJViewport(JViewport jViewport){
        TimingUtils.jViewport = jViewport;
    }

    public static synchronized void removeShowedDayTasks(){
        i--;
    }

    public  synchronized static void registeDayTask(DayTask dayTask){
        synchronized(TimingUtils.class){
            for(DayTask d:dayTasks){
                if(d.getId() == dayTask.getId()){
                    return;
                }
            }
        }
        dayTasks.add(dayTask);
    }

    public synchronized static void start(){
        if(flag){
            flag = false;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
                    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try{
                        while (true){
                            Iterator<DayTask> it = dayTasks.iterator();
                            TimeUnit.SECONDS.sleep(2);
                            while(it.hasNext()){
                                boolean flag = true;
                                DayTask dayTask = it.next();
                                if(dayTask!=null){
                                    for(DayTask showedTask: showedTasks){
                                        if(dayTask == showedTask){
                                            flag = false;
                                        }
                                    }
                                    if(flag){
                                        long theTime = format1.parse(format.format(new Date())+dayTask.getStartDateTime()+":00").getTime();
                                        if(theTime <= System.currentTimeMillis()) {
                                            RemindPanel panel = new RemindPanel(i * 120, dayTask);
                                            i++;
                                            showedTasks.add(dayTask);
                                            TaskPanelUtils.refreshTasksPanel(jViewport,dayTask.getDayDO());
                                        }
                                    }

                                }

                            }

                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }).start();
        }else{
            System.out.println("不能多次启动");
        }
    }


}
